{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "677f3629",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# Arrays and Variables\n",
    "\n",
    "[![Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/brainpy/brainpy/blob/master/docs_version2/tutorial_math/arrays_and_variables.ipynb)\n",
    "[![Open in Kaggle](https://kaggle.com/static/images/open-in-kaggle.svg)](https://kaggle.com/kernels/welcome?src=https://github.com/brainpy/brainpy/blob/master/docs_version2/tutorial_math/arrays_and_variables.ipynb)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e9be090e",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "@[Xiaoyu Chen](mailto:c-xy17@tsinghua.org.cn)\n",
    "@[Chaoming Wang](https://github.com/chaoming0625)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b39bc3a4",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "In this section ,we will briefly introduce two basic and important data structures: arrays and variables. They form the foundation for mathematical operations of brain dynamics programming (BDP) in BrainPy."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "da80070c",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## Arrays"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a5aabd4",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### Definition and Attributes"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "964cf619",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "An array is a data structure that organizes algebraic objects in a multidimentional vector space. Simply speaking, in BrainPy, an array is a multidimensional array that contains the same type of data, most commonly of the numeric or boolean type.\n",
    "\n",
    "The dimensions of an array are called **axes**. In the following illustration, the 1-D array (`[7, 2, 9, 10]`) only has one axis. There are 4 elements in this axis, so the shape of the array is `(4,)`. \n",
    "\n",
    "By contrast, the 2-D array in the illustration has 2 axes. The first axis is of length 2 and the second of length 3. Therefore, the shape of the 2-D array is `(2, 3)`. \n",
    "\n",
    "Similarly, the 3-D array has 3 axes, with the dimensions `(4, 3, 2)` in each axis, respectively. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a27c2e5f",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "<center><img src=\"../_static/numpy_arrays.png\" width=\"600 px\"></center>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0b21690a",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "To enable array operations, users should import the ``brainpy.math`` module:"
   ]
  },
  {
   "cell_type": "code",
   "id": "5f03e5a3",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-10-06T04:00:13.846979Z",
     "start_time": "2025-10-06T04:00:08.949676Z"
    }
   },
   "source": [
    "import brainpy.math as bm\n",
    "\n",
    "# bm.set_platform('cpu')"
   ],
   "outputs": [],
   "execution_count": 1
  },
  {
   "cell_type": "code",
   "id": "20cb90fe",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-10-06T04:00:13.875585Z",
     "start_time": "2025-10-06T04:00:13.857167Z"
    }
   },
   "source": [
    "t1 = bm.array([[[0, 1, 2, 3], [1, 2, 3, 4], [4, 5, 6, 7]], \n",
    "               [[0, 0, 0, 0], [-1, 1, -1, 1], [2, -2, 2, -2]]])\n",
    "t1"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Array(value=Array([[[ 0,  1,  2,  3],\n",
       "                    [ 1,  2,  3,  4],\n",
       "                    [ 4,  5,  6,  7]],\n",
       "            \n",
       "                   [[ 0,  0,  0,  0],\n",
       "                    [-1,  1, -1,  1],\n",
       "                    [ 2, -2,  2, -2]]]),\n",
       "      dtype=int32)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 2
  },
  {
   "cell_type": "markdown",
   "id": "d391e7f9",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "Here we create a 3-dimensional array with the shape of (2, 3, 4) and the type of `int32`. Arrays created by ``brainpy.math`` will be stored in ``JaxArray``, for their future operations will be accelerated by just-in-time (JIT) compilation."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d206a43a",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "A array has several important attributes:\n",
    "\n",
    "- **.ndim**: the number of axes (dimensions) of the array.\n",
    "\n",
    "- **.shape**: the dimensions of the array. This is a tuple of integers indicating the size of the array in each dimension. For a matrix with n rows and m columns, the shape will be `(n,m)`. The length of the shape tuple is therefore the number of axes, `ndim`.\n",
    "\n",
    "- **.size**: the total number of elements of the array. This is equal to the product of the elements of shape.\n",
    "\n",
    "- **.dtype**: an object describing the type of the elements in the array. One can create or specify dtypes using standard Python types."
   ]
  },
  {
   "cell_type": "code",
   "id": "d4a5e216",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-10-06T04:00:13.920726Z",
     "start_time": "2025-10-06T04:00:13.916215Z"
    }
   },
   "source": [
    "print('t1.ndim: {}'.format(t1.ndim))\n",
    "print('t1.shape: {}'.format(t1.shape))\n",
    "print('t1.size: {}'.format(t1.size))\n",
    "print('t1.dtype: {}'.format(t1.dtype))"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "t1.ndim: 3\n",
      "t1.shape: (2, 3, 4)\n",
      "t1.size: 24\n",
      "t1.dtype: int32\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "cell_type": "markdown",
   "id": "6be487b0",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "Below we will give a few examples of array operations that are commonly used in brain dynamics programming. For more details about array operations, please refer to the [array tutorial](array.ipynb)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d4b2c475",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### Creating a array"
   ]
  },
  {
   "cell_type": "code",
   "id": "d2d6d644",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-10-06T04:00:14.005331Z",
     "start_time": "2025-10-06T04:00:13.939476Z"
    }
   },
   "source": [
    "t2 = bm.arange(4)\n",
    "# t2: JaxArray([0, 1, 2, 3], dtype=int32)\n",
    "\n",
    "t3 = bm.ones((2, 4)) * 1.5\n",
    "# t3: JaxArray([[1.5, 1.5, 1.5, 1.5],\n",
    "#               [1.5, 1.5, 1.5, 1.5]], dtype=float32)"
   ],
   "outputs": [],
   "execution_count": 4
  },
  {
   "cell_type": "markdown",
   "id": "a6af56ae",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### Array operations"
   ]
  },
  {
   "cell_type": "code",
   "id": "b7d9fe6c",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-10-06T04:00:14.058416Z",
     "start_time": "2025-10-06T04:00:14.012533Z"
    }
   },
   "source": [
    "# indexing and slicing\n",
    "t3[1]\n",
    "# DeviceArray([1.5, 1.5, 1.5, 1.5], dtype=float32)\n",
    "\n",
    "t3[:, 2:]\n",
    "# DeviceArray([[1.5, 1.5],\n",
    "#              [1.5, 1.5]], dtype=float32)"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Array([[1.5, 1.5],\n",
       "       [1.5, 1.5]], dtype=float32)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 5
  },
  {
   "cell_type": "code",
   "id": "067def1a",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-10-06T04:00:14.190503Z",
     "start_time": "2025-10-06T04:00:14.064220Z"
    }
   },
   "source": [
    "# algebraic operations\n",
    "t2 + t3[0]\n",
    "# JaxArray([1.5, 2.5, 3.5, 4.5], dtype=float32)\n",
    "\n",
    "t3[0] / t1[0, 1]\n",
    "# DeviceArray([1.5  , 0.75 , 0.5  , 0.375], dtype=float32)\n",
    "\n",
    "# broadcasting\n",
    "t2 + 3\n",
    "# JaxArray([3, 4, 5, 6], dtype=int32)\n",
    "\n",
    "t2 + t3\n",
    "# JaxArray([[1.5, 2.5, 3.5, 4.5],\n",
    "#           [1.5, 2.5, 3.5, 4.5]], dtype=float32)"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Array([[1.5, 2.5, 3.5, 4.5],\n",
       "       [1.5, 2.5, 3.5, 4.5]], dtype=float32)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 6
  },
  {
   "cell_type": "code",
   "id": "be731099",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-10-06T04:00:14.274185Z",
     "start_time": "2025-10-06T04:00:14.200700Z"
    }
   },
   "source": [
    "# some functions\n",
    "bm.dot(t2, t3.T)\n",
    "# JaxArray([9., 9.], dtype=float32)\n",
    "\n",
    "bm.max(t1, axis=2)\n",
    "# JaxArray([[3, 4, 7],\n",
    "#           [0, 1, 2]], dtype=int32)\n",
    "\n",
    "t3.flatten()\n",
    "# JaxArray([1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5], dtype=float32)"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Array([1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5], dtype=float32)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 7
  },
  {
   "cell_type": "markdown",
   "id": "adf388d0",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "In BrainPy, arrays can be used to store some parameters related to dynamical models. For example, if we define a group of Integrate-and-Fire (LIF) neurons and wish to assign each neuron with a different time constant $\\tau$, then we can generate an array containing an array of time constants."
   ]
  },
  {
   "cell_type": "code",
   "id": "c1c11eba",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-10-06T04:00:14.519159Z",
     "start_time": "2025-10-06T04:00:14.281277Z"
    }
   },
   "source": [
    "n = 6  # assume there are 6 LIF neurons\n",
    "tau = bm.random.randn(n)*2. + 20.\n",
    "tau"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Array([20.199146, 23.244438, 20.915506, 20.84281 , 22.016336, 18.12661 ],      dtype=float32)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 8
  },
  {
   "cell_type": "markdown",
   "id": "309a04b0",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "Through the code above, a group of time constants is generated from a normal distribution, with a mean of 20 and a variance of 2."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6cd5e9bf",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## Variables"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "df4fa849",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "We have talked about the definition, operations, and application of arrays in BrainPy. There are some situations, however, where arrays are not applicable. Due to [JIT compilation](jit_compilation.ipynb), once a array is given to the JIT compiler, the values inside the array cannot be changed. This gives rise to severe limitations, because some properties of the dynamical system, such as the membrane potential, dynamically changes over time. Therefore, we need a new data structure to store such dynamic variables, and that is ``brainpy.math.Variable``."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "95334da1",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### ``brainpy.math.Variable``"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dadd6b45",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "``brainpy.math.Variable`` is a pointer referring to a array. The array is stored as its value. The data in a Variable can be changed during JIT compilation. **If a array is labeled as a Variable, it means that it is a dynamical variable that changes over time.**"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "27c7f7a1",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "To create or change a array into a variable, users just need to wrap the array into ``brainpy.math.Variable``:"
   ]
  },
  {
   "cell_type": "code",
   "id": "775db063",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-10-06T04:00:14.532518Z",
     "start_time": "2025-10-06T04:00:14.527171Z"
    }
   },
   "source": [
    "v = bm.Variable(t2)\n",
    "v"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Variable(\n",
       "  value=ShapedArray(int32[4]),\n",
       "  _batch_axis=None,\n",
       "  axis_names=None\n",
       ")"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 9
  },
  {
   "cell_type": "markdown",
   "id": "01e6f148",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "Note that the array is contained in a \"Variable\" instead of a \"JaxArray\"."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2842e288",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "```{note}\n",
    "Arrays that are not marked as Variables will be JIT compiled as static data. In [JIT compilation](jit_compilation.ipynb), it is shown that modifications of arrays are invalid in a JIT-compilation environment.\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8905e1bd",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "Users can access the value in the Variable through its attribute `.value`:"
   ]
  },
  {
   "cell_type": "code",
   "id": "a90a833e",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-10-06T04:00:14.561779Z",
     "start_time": "2025-10-06T04:00:14.556843Z"
    }
   },
   "source": [
    "v.value"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Array([0, 1, 2, 3], dtype=int32)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 10
  },
  {
   "cell_type": "markdown",
   "id": "35c0bf25",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "Since the data inside a Variable is a array, common operations on arrays can be directly grafted to Variables."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f41a846",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## In-place updating"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "02531880",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "Though the operations are the same, there are some requirements for updating a Variable. If we directly change a Variable, The returning data will become a array but not a Variable."
   ]
  },
  {
   "cell_type": "code",
   "id": "38170c78",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-10-06T04:00:14.591076Z",
     "start_time": "2025-10-06T04:00:14.586689Z"
    }
   },
   "source": [
    "v2 = v + 2\n",
    "v2"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Array([2, 3, 4, 5], dtype=int32)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 11
  },
  {
   "cell_type": "markdown",
   "id": "6805368a",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "To update the Variable, users are required to use in-place updating, which only modifies the value inside the Variable but does not change the reference pointing to the Variable. In-place updating operations include:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cccb2553",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "**1\\. Indexing and slicing**\n",
    "\n",
    "  - Indexing: ``v[i] = a``\n",
    "  - Slicing: ``v[i:j] = b``\n",
    "  - Slicing the specific values: ``v[[1, 3]] = c``\n",
    "  - Slicing all values, ``v[:] = d``, ``v[...] = e``\n",
    "\n",
    "for more details, please refer to [Array Objects Indexing](https://numpy.org/doc/stable/reference/arrays.indexing.html)."
   ]
  },
  {
   "cell_type": "code",
   "id": "0f0c7b60",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-10-06T04:00:14.712449Z",
     "start_time": "2025-10-06T04:00:14.606467Z"
    }
   },
   "source": [
    "v[0] = 10\n",
    "v[1:3] = 9\n",
    "v"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Variable(\n",
       "  value=ShapedArray(int32[4]),\n",
       "  _batch_axis=None,\n",
       "  axis_names=None\n",
       ")"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 12
  },
  {
   "cell_type": "markdown",
   "id": "cbedbc1b",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "**2\\. Augmented assignment**\n",
    "\n",
    "- ``+=`` (add)\n",
    "- ``-=`` (subtract)\n",
    "- ``/=`` (divide)\n",
    "- ``*=`` (multiply)\n",
    "- ``//=`` (floor divide)\n",
    "- ``%=`` (modulo)\n",
    "- ``**=`` (power)\n",
    "- ``&=`` (and)\n",
    "- ``|=`` (or)\n",
    "- ``^=`` (xor)\n",
    "- ``<<=`` (left shift)\n",
    "- ``>>=`` (right shift)"
   ]
  },
  {
   "cell_type": "code",
   "id": "96e5fd0e",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-10-06T04:00:14.782945Z",
     "start_time": "2025-10-06T04:00:14.734437Z"
    }
   },
   "source": [
    "v -= 3\n",
    "v <<= 1\n",
    "v"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Variable(\n",
       "  value=ShapedArray(int32[4]),\n",
       "  _batch_axis=None,\n",
       "  axis_names=None\n",
       ")"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 13
  },
  {
   "cell_type": "markdown",
   "id": "1ef372a7",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "**3\\. ``.value`` assignment**"
   ]
  },
  {
   "cell_type": "code",
   "id": "1c245c84",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-10-06T04:00:14.793788Z",
     "start_time": "2025-10-06T04:00:14.788783Z"
    }
   },
   "source": [
    "v.value = bm.arange(4)\n",
    "v"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Variable(\n",
       "  value=ShapedArray(int32[4]),\n",
       "  _batch_axis=None,\n",
       "  axis_names=None\n",
       ")"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 14
  },
  {
   "cell_type": "markdown",
   "id": "a81a9857",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "`` .value`` assignment directly accesses the data stored in the JaxArray. When using `.value`, the new data should be of the same type and shape as the original ones."
   ]
  },
  {
   "cell_type": "code",
   "id": "4038eb25",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-10-06T04:00:14.856567Z",
     "start_time": "2025-10-06T04:00:14.842485Z"
    }
   },
   "source": [
    "try:\n",
    "    v.value = bm.array([1., 1., 1., 0.])\n",
    "except Exception as e:\n",
    "    print(type(e), e)"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'brainpy.MathError'> The dtype of the original data is int32, while we got float32.\n"
     ]
    }
   ],
   "execution_count": 15
  },
  {
   "cell_type": "markdown",
   "id": "0bcbce40",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "**4\\. ``.update()`` method**\n",
    "\n",
    "This method will also check if the new data is of the same type and shape as the original ones."
   ]
  },
  {
   "cell_type": "code",
   "id": "0d4e67ae",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-10-06T04:00:14.880794Z",
     "start_time": "2025-10-06T04:00:14.864177Z"
    }
   },
   "source": [
    "v.update(bm.array([3, 4, 5, 6]))\n",
    "v"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Variable(\n",
       "  value=ShapedArray(int32[4]),\n",
       "  _batch_axis=None,\n",
       "  axis_names=None\n",
       ")"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 16
  },
  {
   "cell_type": "markdown",
   "id": "4e0b9e6c",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "For more details, such as the subtypes of Variables and more information about in-place updating, please see the [advanced tutorial for Variables](../tutorial_math/variables.ipynb)."
   ]
  },
  {
   "cell_type": "code",
   "id": "e4637fbd",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-10-06T04:00:14.899577Z",
     "start_time": "2025-10-06T04:00:14.896677Z"
    }
   },
   "source": [],
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
